home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / winsock / ircii2-6.zip / SRC\IRCII-2.6\SOURCE\WSERV.C < prev    next >
C/C++ Source or Header  |  1994-12-28  |  3KB  |  148 lines

  1. /*
  2.  * wserv.c - little program to be a pipe between a screen or
  3.  * xterm window to the calling ircII process.
  4.  *
  5.  * Copyright Troy Rollo, 1992.
  6.  * 
  7.  * Finished by Matthew Green, 1993.
  8.  *
  9.  * Works by opening up the unix domain socket that ircII bind's
  10.  * before calling wserv, and which ircII also deleted after the
  11.  * connection has been made.
  12.  */
  13.  
  14. #ifndef lint
  15. static    char    rcsid[] = "@(#)$Id: wserv.c,v 1.13 1994/07/02 02:32:13 mrg Stab $";
  16. #endif
  17.  
  18. #include "defs.h"
  19.  
  20. #ifdef HAVE_SYS_UN_H
  21.  
  22. # include "irc.h"
  23. # include "term.h"
  24.  
  25. # include <sys/un.h>
  26.  
  27. /*
  28.  * Holds the parent process id, as taken from the command line arguement.
  29.  * We can't use getppid() here, because the parent for screen is the
  30.  * underlying screen process, and for xterm's, it the xterm itself.
  31.  */
  32. static    pid_t    ircIIpid;
  33.  
  34. /* declare the signal handler */
  35. # if !defined(_RT) && defined(SIGWINCH)
  36. void    got_sigwinch();
  37. # endif /* _RT */
  38.  
  39. void
  40. main(argc, argv)
  41.     int    argc;
  42.     char    **argv;
  43. {
  44.     char    buffer[1024];
  45.     struct    sockaddr_un *addr = (struct sockaddr_un *) buffer;
  46.     int    nread;
  47.     fd_set    reads;
  48.     int    s;
  49.     char    *path,
  50.         *tmp;
  51.  
  52.     /* Set up the signal hander to pass SIGWINCH to ircII */
  53. # if !defined(_RT) && defined(SIGWINCH)
  54.     (void) MY_SIGNAL(SIGWINCH, got_sigwinch, 0);
  55. # endif /* _RT */
  56.  
  57.     if (2 != argc)    /* no socket is passed */
  58.         exit(0);
  59.     /*
  60.      * First thing we do here is grab the parent pid from the command
  61.      * line arguements, because getppid() returns the wrong pid in
  62.      * all cases.. is comes in via the command line arguement in the
  63.      * for of irc_xxxxxxxx .. as an 8 digit decimal number..  if we
  64.      * can't get the pid from the command line arg, then we set it
  65.      * to -1, which is used later to ignore them..
  66.      */
  67.  
  68.     path = (char *) malloc(strlen(argv[1]) + 1);
  69.     strcpy(path, argv[1]);
  70.     if ((char *) 0 != (tmp = (char *) index(path, '_')))
  71.         ircIIpid = atoi(++tmp);
  72.     else
  73.         ircIIpid = -1;
  74.  
  75.     /*
  76.      * Set up the socket, from the path passed, connect it.. all that
  77.      * stuff..  And initalise the term settings for the window.
  78.      */
  79.     addr->sun_family = AF_UNIX;
  80.     strcpy(addr->sun_path, argv[1]);
  81.     s = socket(AF_UNIX, SOCK_STREAM, 0);
  82.     if (0 > connect(s, (struct sockaddr *) addr, sizeof(addr->sun_family) +
  83.                         strlen(addr->sun_path)))
  84.         exit(0);
  85.  
  86.     /*
  87.      * first line to for a wserv program is the tty.  this is so ircii
  88.      * can grab the size of the tty, and have it changed.
  89.      */
  90.  
  91.     tmp = ttyname(0);
  92.     write(s, tmp, strlen(tmp));
  93.     write(s, "\n", 1);
  94.     perror(tmp);
  95.  
  96.     term_init();
  97.  
  98.     /*
  99.      * The select call..  reads from the socket, and from the window..
  100.      * and pipes the output from out to the other..  nice and simple
  101.      */
  102.     while (1)
  103.     {
  104.         FD_ZERO(&reads);
  105.         FD_SET(0, &reads);
  106.         FD_SET(s, &reads);
  107.         select(s + 1, &reads, (fd_set *) 0, (fd_set *) 0,
  108.             (struct timeval *) 0);
  109.         if (FD_ISSET(0, &reads))
  110.         {
  111.             if (0 != (nread = read(0, buffer, sizeof(buffer))))
  112.                 write(s, buffer, nread);
  113.             else
  114.                 exit(0);
  115.         }
  116.         if (FD_ISSET(s, &reads))
  117.         {
  118.             if (0 != (nread = read(s, buffer, sizeof(buffer))))
  119.                 write(0, buffer, nread);
  120.             else
  121.                 exit(0);
  122.         }
  123.     }
  124. }
  125.  
  126. /* got_sigwinch: we got a SIGWINCH, so we send it back to ircII */
  127. # if !defined(_RT) && defined(SIGWINCH)
  128. void
  129. got_sigwinch()
  130. {
  131. #  ifdef SYSVSIGNALS
  132.     (void) MY_SIGNAL(SIGWINCH, got_sigwinch, 0);
  133. #  endif
  134.     if (-1 != ircIIpid)
  135.         kill(ircIIpid, SIGWINCH);
  136. }
  137. # endif /* _RT */
  138.  
  139. #else
  140.  
  141. void
  142. main()
  143. {
  144.     exit(0);
  145. }
  146.  
  147. #endif /* HAVE_SYS_UH_H */
  148.